//
//  RaiseIndexViewController.m
//  Kjs-iOS
//
//  Created by 江伟 on 16/8/1.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "RaiseIndexViewController.h"
#import "GdItemTypeCell.h"
#import "RaiseDetailViewController.h"
#import "AppDelegate.h"
#import "GUATool.h"
#import "ProtocolViewController.h"
#import "AddBankCardViewController.h"
#import "RealAuthViewController.h"
#import "ReSetTradePswViewController.h"
#import "KJSRequest.h"
#import "UIColor+KjsColor.h"
#define ProFontSize 10

@interface RaiseIndexViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,weak) IBOutlet UIButton *confirmBtn;
@property (nonatomic,weak) IBOutlet UITableView *gdItemTypetableV;
@property (nonatomic,weak) IBOutlet UIView *protocolContainV;
//@property (nonatomic,weak) IBOutlet UILabel *protocolLab;
//@property (nonatomic,strong) NSString *protocolTitle;
@property (nonatomic,weak) IBOutlet UIButton *checkBtn;
@property (nonatomic,assign) Boolean check;
@property (nonatomic,assign) int gdItemType;

@property (nonatomic,weak) IBOutlet UILabel *ifRealAuthLab;
@property (nonatomic,weak) IBOutlet UIImageView *ifRealAuthImageV;

@property (nonatomic,weak) IBOutlet UILabel *ifSetPswLab;
@property (nonatomic,weak) IBOutlet UIImageView *ifSetPswImageV;

@property (nonatomic,weak) IBOutlet UILabel *ifBindCardLab;
@property (nonatomic,weak) IBOutlet UIImageView *ifBindCardImageV;

@property (nonatomic,weak) IBOutlet NSLayoutConstraint *titleHeigh;
@property (nonatomic,weak) IBOutlet NSLayoutConstraint *authHeigh;
@property (nonatomic,assign) float initTitleHeigh;
@property (nonatomic,assign) float initAuthHeigh;
@property (nonatomic,weak) IBOutlet NSLayoutConstraint *tableViewHeigh;

@property (nonatomic,strong) PRealAuth *pRealAuthInfo;
@property (nonatomic,strong) ORealAuth *oRealAuthInfo;

@property (nonatomic,strong) NSArray <NSString *> *proTypeArray;
@property (nonatomic,strong) NSArray *proFlagArray;
@end

@implementation RaiseIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我要融资";
    self.naviTopView = YES;
    self.gdItemType = -1;
    
    self.gdItemTypetableV.delegate = self;
    self.gdItemTypetableV.dataSource = self;
    
    self.confirmBtn.layer.cornerRadius = 15;
    self.confirmBtn.layer.masksToBounds = YES;
    
     self.gdItemTypetableV.tableHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"RaiseIndexHeaderView" owner:self options:nil] firstObject];
    //协议颜色
//    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",self.protocolLab.text]];
//    NSRange contentRange = {0,[content length]};
//    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
//    self.protocolLab.attributedText = content;
//    UITapGestureRecognizer *goRaisePro = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goProtocolViewController)];
//    self.protocolLab.userInteractionEnabled = YES;
//    [self.protocolLab addGestureRecognizer:goRaisePro];

    [self.checkBtn addTarget:self action:@selector(isCheck) forControlEvents:UIControlEventTouchUpInside];
    self.check = true;
    [self.checkBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-checked"] forState:UIControlStateNormal];
    
    [self.confirmBtn addTarget:self action:@selector(goRaiseDetail) forControlEvents:UIControlEventTouchUpInside];
    
    self.initAuthHeigh = self.authHeigh.constant;
    self.initTitleHeigh = self.titleHeigh.constant;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.authHeigh.constant = self.initAuthHeigh;
    self.titleHeigh.constant = self.initTitleHeigh;
    NSLog(@"用户等级＝%d",[CurentDelegate.account.levels intValue]);
    if (!CurentDelegate.account){
        [GUATool showDialog:@"请先登录" message:nil waitingTime:1.5];
    }else{
        if ([CurentDelegate.account.levels intValue] > 0) {
            [self.ifRealAuthImageV setImage:[UIImage imageNamed:@"invest-approve"]];
            self.ifRealAuthLab.text = @"已认证";
            self.ifRealAuthLab.userInteractionEnabled = NO;
        }else{
            [self.ifRealAuthImageV setImage:[UIImage imageNamed:@"invest-unapprove"]];
            self.ifRealAuthLab.text = @"未认证";
            UITapGestureRecognizer *goAuth = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goRealAuth)];
            self.ifRealAuthLab.userInteractionEnabled = YES;
            [self.ifRealAuthLab addGestureRecognizer:goAuth];
        }
        
        if ([CurentDelegate.account.levels intValue] > 1) {
            [self.ifSetPswImageV setImage:[UIImage imageNamed:@"invest-approve"]];
            self.ifSetPswLab.text = @"已设置";
            self.ifSetPswLab.userInteractionEnabled = NO;
            
        }else{
            [self.ifSetPswImageV setImage:[UIImage imageNamed:@"invest-unapprove"]];
            self.ifSetPswLab.text = @"未设置";
            UITapGestureRecognizer *goSet = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goSetTradePsw)];
            self.ifSetPswLab.userInteractionEnabled = YES;
            [self.ifSetPswLab addGestureRecognizer:goSet];
        }
        
        if ([CurentDelegate.account.levels intValue] > 2) {
            [self.ifBindCardImageV setImage:[UIImage imageNamed:@"invest-approve"]];
            self.titleHeigh.constant = 0;
            self.authHeigh.constant = 0;
            self.ifBindCardLab.userInteractionEnabled = NO;
            self.ifBindCardLab.text = @"已绑定";
        }else{
            [self.ifBindCardImageV setImage:[UIImage imageNamed:@"invest-unapprove"]];
            self.ifBindCardLab.text = @"未绑定";
            self.ifBindCardLab.userInteractionEnabled = YES;
            UITapGestureRecognizer *goBind = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goBindBankCard)];
            [self.ifBindCardLab addGestureRecognizer:goBind];
            
        }
    }
    [self.gdItemTypetableV reloadData];
    [self vaildRealAuth];
    
    
}
#pragma mark - custom method
//画协议
-(void)drawProtocol
{
    //清楚已有内容，重绘
    [self.protocolContainV.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSString *firstStr = self.proTypeArray.firstObject;
    float x = 0;
    float y = 0;
    float width = firstStr.length * ProFontSize ;
    NSLog(@"数组长度=%lu",[self.proTypeArray count]);
    for(int i = 0; i < [self.proTypeArray count] ; i++)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, ProFontSize)];
        label.text = self.proTypeArray[i];
        label.font = [UIFont systemFontOfSize:ProFontSize];
        label.textColor = [UIColor kjsMainColor];
        UITapGestureRecognizer *goRaisePro = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goProtocolViewController:)];
        label.userInteractionEnabled = YES;
        label.tag = i;
        [label addGestureRecognizer:goRaisePro];

        [self.protocolContainV addSubview:label];
        if ((i+1) < [self.proTypeArray count]){
            NSLog(@"i+1=%d",i+1);
            x = x + width;
            if ((x + self.proTypeArray[i+1].length * ProFontSize) > (self.protocolContainV.frame.size.width)) {
                x = 0 ;
                y = y + ProFontSize;
            }
            width = self.proTypeArray[i+1].length * ProFontSize;
        }
    }
}

-(void)vaildRealAuth
{
    if (CurentDelegate.account) {
        [self GetPRealInfo];
    }
}
-(void)GetPRealInfo
{
    [KJSRequest GetPRealAuthInfoWithPartyId:CurentDelegate.account.partyId ret:^(PRealAuthResultModel *model) {
        self.pRealAuthInfo = model.realAuthInfo;
        if ([self.pRealAuthInfo.auditingOpinion isEqualToString:@"0"]) {
            //审核不通过
            self.authHeigh.constant = self.initAuthHeigh;
            self.titleHeigh.constant = self.initTitleHeigh;
            [self.ifRealAuthImageV setImage:[UIImage imageNamed:@"invest-unapprove"]];
            self.ifRealAuthLab.text = @"未认证";
            UITapGestureRecognizer *goAuth = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goRealAuth)];
            self.ifRealAuthLab.userInteractionEnabled = YES;
            [self.ifRealAuthLab addGestureRecognizer:goAuth];
        }
    }];
}
-(void)goRealAuth
{
    RealAuthViewController *realAuthVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"RealAuthViewController"];
    [self displayViewController:realAuthVC];
}
-(void)goSetTradePsw
{
    if ([CurentDelegate.account.levels integerValue] == 0){
        [GUATool showDialog:@"请先实名认证" message:nil waitingTime:1.5];
    }else{
        ReSetTradePswViewController *reSetTradePswlVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ReSetTradePswViewController"];
        [self displayViewController:reSetTradePswlVC];
    }
}
-(void)goBindBankCard
{
    if ([CurentDelegate.account.levels integerValue] < 2) {
        [GUATool showDialog:@"请先设置交易密码" message:nil waitingTime:1.5];
    }else{
        AddBankCardViewController *addBankCardVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"AddBankCardViewController"];
        [self displayViewController:addBankCardVC];
    }

}
-(NSArray *)getItemTypeNameArray
{
    NSLog(@"%@",CurentDelegate.account.type);
    if ([CurentDelegate.account.type isEqualToString:@"p"]) {
        return @[@"物权质押贷",@"普惠三农贷",@"消费金融贷款"];
    }else{
        return NULL;
    }
}
-(void)isCheck{
    self.check = !self.check;
    if (self.check) {
        NSLog(@"选中");
        [self.checkBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-checked"] forState:UIControlStateNormal];
    }else{
        NSLog(@"no选中");
        [self.checkBtn setBackgroundImage:[UIImage imageNamed:@"checkbox-unchecked"] forState:UIControlStateNormal];
    }
   
}
-(void)goRaiseDetail
{
    if (!CurentDelegate.account) {
        [GUATool showDialog:@"请先登录" message:nil waitingTime:1.5];
    }else if ([CurentDelegate.account.levels intValue] < 1 || [self.pRealAuthInfo.auditingOpinion isEqualToString:@"0"]){
        [GUATool showDialog:@"请先进行实名认证" message:nil waitingTime:1.5];
    }else if ([CurentDelegate.account.levels intValue] < 2){
        [GUATool showDialog:@"请先设置交易密码" message:nil waitingTime:1.5];
    }
    else if ([CurentDelegate.account.levels intValue] < 3){
        [GUATool showDialog:@"请先绑定银行卡" message:nil waitingTime:1.5];
    }
    else if (self.gdItemType == -1) {
        [GUATool showDialog:@"请选择项目类型" message:nil waitingTime:1.5];
    }else if(!self.check){
        [GUATool showDialog:@"请勾选协议" message:nil waitingTime:1.5];
    }else{
        RaiseDetailViewController *raiseDetailVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"RaiseDetailViewController"];
        raiseDetailVC.gdItemType = self.gdItemType;
        [self displayViewController:raiseDetailVC];
    }
    
}
-(void)goProtocolViewController:(id)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    NSLog(@"tap.tag= %ld",tap.view.tag);
    ProtocolViewController *protocolVC = [[UIStoryboard storyboardWithName:@"Seconed" bundle:nil] instantiateViewControllerWithIdentifier:@"ProtocolViewController"];
    protocolVC.proType = [NSString stringWithFormat:@"%@",self.proFlagArray[tap.view.tag]];
    protocolVC.proTitle = self.proTypeArray[tap.view.tag];
    [self displayViewController:protocolVC];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self getItemTypeNameArray] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GdItemTypeCell *cell = [self.gdItemTypetableV dequeueReusableCellWithIdentifier:@"GdItemTypeCell"];
    cell.typeLab.text = [self getItemTypeNameArray][indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 43;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中%ld",(long)indexPath.row);
        self.gdItemType = (int)indexPath.row + 2;
        switch (indexPath.row) {
            case 0:
                self.proTypeArray = WQZYDPro;
                self.proFlagArray = WQZYDProFlag;
                break;
            case 1:
                self.proTypeArray = PHSNDPro;
                self.proFlagArray = PHSNDProFlag;
                break;
            case 2:
                self.proTypeArray = XFJRDPro;
                self.proFlagArray = XFJRDProFlag;
                break;
            default:
                break;
        }
        [self drawProtocol];
   
}


@end
